package com.sx.manage.service.company.type.change;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.manage.service.company.IMCompanyTypeChangeService;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.company.MCompanyTypeChangeForm;
import com.sx.model.manage.mybatis.vo.company.MCompanyTypeChangeVO;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
/**
  *公司属性修改记录表
  *@author Administrator
  *@version 1.0.0 Tue Sep 19 00:00:00 CST 2017 
  */
@Service("companyTypeChangeWebServiceImpl") 
public class MCompanyTypeChangeWebServiceImpl  { 
	/*** 日志*/ 
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	
	//公司属性编辑
	@Resource(name="companyTypeChangeServiceImpl")
	private IMCompanyTypeChangeService companyTypeChangeServiceImpl;
	
	/** 公司 **/
	public ResultBooleanVO companyTypeChange(MCompanyVO oldCompany ,MCompanyVO newCompany,MConsumerLoginVO loginConsumer){				
		_log.debug("公司属性修改方法调用，首先获取公司属性判断是否修改");
		if( StringUtils.isNotBlank( oldCompany.getCompany_type())
				&& StringUtils.isNotBlank( newCompany.getCompany_type())
				){
			if(oldCompany.getCompany_type().equals( newCompany.getCompany_type())){
				_log.debug("公司属性没有修改,直接返回");
				return new ResultBooleanVO();
			}
			_log.debug("公司属性有修改开始记录，old_company_type={},new_company_type={}",oldCompany.getCompany_type(),newCompany.getCompany_type());
			MCompanyTypeChangeForm  form = new MCompanyTypeChangeForm();
			                        form.setCompany_id( newCompany.getId() );
			                        form.setCreate_time( new Date());
			                        form.setData_status( MDataStatusType.DATA_NEW_ADD);
			                        
			ResultListVO<MCompanyTypeChangeVO> changeResult =  companyTypeChangeServiceImpl.queryList(form);
			//新增状态的数据每个公司每日只能有一个
			if( changeResult.isSuccess() ){
				if( null != changeResult.getData() && changeResult.getData().size()==1 ){
					_log.debug("公司今日有数据，修改原数据");
					MCompanyTypeChangeVO changeVO = changeResult.getData().get(0);
					MCompanyTypeChangeForm updateForm = new MCompanyTypeChangeForm();
											updateForm.setId( changeVO.getId() );											
											updateForm.setCompany_type_new( newCompany.getCompany_type() );	
											updateForm.setRemark("公司属性修改，二次修改");
					companyTypeChangeServiceImpl.save(updateForm, loginConsumer);
				}else if(null == changeResult.getData()||changeResult.getData().isEmpty()){
					_log.debug("公司今日无数据，新增数据");
					MCompanyTypeChangeForm insertForm = new MCompanyTypeChangeForm();
											insertForm.setCompany_id( newCompany.getId() );
											insertForm.setCompany_name( newCompany.getCompany_name());
											insertForm.setCompany_type_new( newCompany.getCompany_type() );
											insertForm.setCompany_type_old( oldCompany.getCompany_type() );
											insertForm.setCreate_time( new Date());
											insertForm.setData_status( MDataStatusType.DATA_NEW_ADD );
											insertForm.setRemark("公司属性修改新增数据");
					companyTypeChangeServiceImpl.save(insertForm, loginConsumer);
				}
			}
		}else{
			_log.debug("公司属性为空，old_company_type={},new_company_type={}",oldCompany.getCompany_type(),newCompany.getCompany_type());
			return new ResultBooleanVO(false,null,"公司属性为空,请检查数据");
		}	
		 return new ResultBooleanVO();
	}
} //end class